
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm.session import sessionmaker


# 连接mysql
db_connect_string = '{database_type}+{driver}://{username}:{password}@{host}:{port}/{db}'.format(
    database_type='mysql', driver='pymysql', username='root', password='python123',
    host='127.0.0.1', port=3306, db='sqlalchemy_01'
)

# 创建引擎
engine = create_engine(db_connect_string)


# 创建orm对象
Base_Models = declarative_base()


# 创建session 对象
Session = sessionmaker(bind=engine)
session = Session()


def init_db():
    """
    初始化数据库
    """
    Base_Models.metadata.create_all(engine)


def drop_db():
    """
    删除数据库
    """
    Base_Models.metadata.drop_all(engine)